<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Node;
use think\Validate;
use think\Db;
use think\Request;
use app\admin\controller\Main;
class Nodes extends Main
{
	//显示权限
	public function nodeList()
	{

        $res = Node::where(true)->paginate(4);
		
		$count = Node::count();
		$this->assign('list',$res);
		$this->assign('count',$count);
		return $this->fetch('node/admin-rule');
	}

	//添加权限
	public function addNode()
	{
		$data = input('post.');
		$validate = new Validate([
			 	'node_name'  => 'require|min:1',
			    'node_cname'  => 'require|min:1',
			    'node_mname' => 'require|min:1'
		]);


		if (!$validate->check($data)) {
			$arr = ['status'=>'0','message'=>$validate->getError()];
		}else{
			$node = Node::create($data);
			if($node->node_id){
    			$arr = ['status'=>'1','message'=>'添加成功'];
    		}else{
    			$arr = ['status'=>'0','message'=>'添加失败'];
    		}
		}	

		if ($arr['status'] == 0) {
            return $this->error($arr['message'],url('admin/Nodes/nodeList'));
        } else {
            return $this->success($arr['message'], url('admin/Nodes/nodeList'));
        }
	}

	//显示编辑页面
	public function showEdit($id)
	{
		$res = Node::get($id);
		$this->assign('list',$res);
		return $this->fetch('node/node-edit');
	}
	//编辑Node
	public function nodeEdit($id)
	{
		$data = input('post.');
		$validate = new Validate([
			 	'node_name'  => 'require|min:1',
			    'node_cname'  => 'require|min:1',
			    'node_mname' => 'require|min:1'
		]);

		$arr = ['status'=>'1','message'=>'修改成功'];
		if (!$validate->check($data)) {
			$arr = ['status'=>'0','message'=>$validate->getError()];
		}else{
			$node = Node::where('node_id', $id)
    				->update($data);
		}	
		if ($arr['status'] == 0) {
            return $this->error($arr['message']);
        } else {
            return $this->success($arr['message'], url('admin/Nodes/nodeList'));
        }

	}
	// 根据id进行删除
	public function delNode($id)
	{
		$result = Db::name('node')->delete($id);

		 if ($result > 0) {
		 	Db::name('role_node')->where('nid',$id)->delete($id);
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的权限删除成功!';
        } else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的权限删除失败!';
        }
        	return json($info);
	}

	//批量删除
}